package com.tgy.twoDatabase.service.impl;

import com.tgy.twoDatabase.dao.master.MsmDao;
import com.tgy.twoDatabase.entity.Sms;
import com.tgy.twoDatabase.entity.SentMessage;
import com.tgy.twoDatabase.service.MsmService;
import com.tgy.twoDatabase.util.RedisNormlUtil;
import com.tgy.twoDatabase.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (sms.a_sms_wait_send)表服务接口实现
 *
 * @author tgy
 * @since 2021-09-27 14:35:30
 */
@Service("msmService")
@Slf4j
public class MsmServiceImpl implements MsmService {

    @Resource
    private MsmDao msmDao;
    @Resource
    private RedisNormlUtil redisNormlUtil;


    public Map<String,Object> insert(Sms sms) {
        Map<String, Object> map = new HashMap<String, Object>();

        //获取现在距离1970年的毫秒时间
        long nowTs = System.currentTimeMillis();
//        应该被删除的时间点
        long delTime = nowTs+(1*1*20*1000);
        if (RedisUtil.ListOps.lIndex("tel"+ sms.getNumber(),0) != null){
            System.out.println(Long.parseLong(RedisUtil.ListOps.lIndex("tel"+ sms.getNumber(),0)));
            System.out.println(Long.parseLong(RedisUtil.ListOps.lIndex("tel"+ sms.getNumber(),0)) < nowTs);
        }

        if (RedisUtil.ListOps.lIndex("tel"+ sms.getNumber(),0) != null  && Long.parseLong(RedisUtil.ListOps.lIndex("tel"+ sms.getNumber(),0)) < nowTs){
            RedisUtil.ListOps.lLeftPop("tel"+ sms.getNumber());
        }
        //限制10秒两条
        if(RedisUtil.ListOps.lSize("tel"+ sms.getNumber())< 100) {
            int successCode = 0;
            map.put("errorMsg", "");
            try {
                //RedisUtil.ZSetOps.zRemoveRangeByScore("tel"+ msm.getNumber(),0,nowTs-(1*1*60*1000));

                //超时时间为60秒
                redisNormlUtil.lSet("tel"+ sms.getNumber(),delTime,180);
                successCode = msmDao.insert(sms);
            } catch (Exception e) {
                e.printStackTrace();
                RedisUtil.ListOps.lRightPop("tel"+ sms.getNumber());//插入出异常则删除
                map.replace("errorMsg", e.toString());
            } finally {
                if (successCode == 1) {
                    map.put("code", successCode);
                    map.put("success", "发送成功");
                } else {
                    map.put("code", 0);
                    map.put("success", "发送失败");
                }
                return map;
            }
        }else{
            map.put("code", 0);
            map.put("success", "号码"+ sms.getNumber()+"短信频繁操作"+"还有"+(Long.parseLong(RedisUtil.ListOps.lIndex("tel"+ sms.getNumber(),0))-nowTs)/1000+"秒");

            log.info("号码"+ sms.getNumber()+"短信频繁操作"+"还有"+(Long.parseLong(RedisUtil.ListOps.lIndex("tel"+ sms.getNumber(),0))-nowTs)/1000+"秒");
            return map;
        }
    }

    @Override
    public Map<String, Object> queryAll( ) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Sms> list = msmDao.queryAll();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("result", list);
        return map;
    }



    @Override
    public Map<String, Object> queryById(Sms sms) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Sms> list = msmDao.queryById(sms);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("result", list);
        return map;
    }

    @Override
    public Map<String, Object> querySentMessage(SentMessage sentMessage) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<SentMessage> list = new ArrayList<>();
        try{
            list = msmDao.querySentMessage(sentMessage);
        }
        catch (Exception e) {
            e.printStackTrace();
            map.put("errorMsg",e.getMessage());
        }
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("result", list);
        return map;
    }

    @Override
    public Map<String, Object> querySent(Map<String, String> mapIn) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<SentMessage> list = new ArrayList<>();
        try{
            list = msmDao.querySent(mapIn);
        }
        catch (Exception e) {
            e.printStackTrace();
            map.put("errorMsg",e.getMessage());
        }
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("result", list);
        return map;
    }
}
